package com.financing.controller.user;
import java.util.Date;
import java.util.Map;

import com.financing.model.Data;
import com.financing.model.user.Bank;
import com.financing.service.user.BankService;
import com.financing.util.Json;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**
 *  银行控制器
 * @since 2017-06-06
 * @author 无处不在的二叔
 */
public class BankController extends Controller {
	private static BankService bank = new BankService();

	/**
	 * 银行列表
	 */
	@Clear
	public void list() { 
		Map<String,String> params = Data.flat(getParaMap());
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10); 
		Page<Bank> p = bank.list(page,size,params);
		renderJson(Json.ok("数据获取成功！", p));
	}	
	
	/**
	 * 银行信息编辑
	 */
	public void edit() { 
		int id = getParaToInt("id",0);
		Bank b = id>0 ? Bank.me.findById(id) : new Bank();
		b.setName(getPara("name"));
		b.setCode(getPara("code"));
		b.setDay(getPara("day","0"));
		b.setLogo(getPara("logo"));
		b.setSingle(getPara("single","0")); 
		b.setStatus(getPara("status","enable"));
		
		if(id>0 ? b.update() : b.setCreateTime(new Date()).save()){
			renderJson(Json.ok("银行信息编辑成功！")); 
		}else{
			renderJson(Json.error("银行信息编辑失败！"));
		}
	}	
	 
	/**
	 * 查看信息
	 */
	public void info() { 
		Bank b = Bank.me.findById(getParaToInt("id",0));
		renderJson(Json.ok("数据获取成功！", Data.put("item",b)));
	}	
	
	/**
	 * 银行信息删除 
	 * 
	 */
	public void del() {
		Bank b = Bank.me.findById(getParaToInt("id",0));
		if(b!=null){
			b.delete();
			renderJson(Json.ok("删除成功！"));
		}
	}
}
 